<template>
	<view  class="search-box">
		<!--使用uni-ui提供的搜索组件-->
		<uni-search-bar @input="input" :radius="100"  cancel-button="none" ></uni-search-bar>
	</view>
	<!--搜索列表-->
	<view class="sugg-list" v-if="searchReuslts.length!==0">
		<view class="sugg-item" v-for="(itemi ,i) in searchReuslts" :key="i" @click="gotoDetail(itemi)">
			<view class="goods-name">{{itemi.goods_name}}</view>
			<uni-icons type="arrow-right" size="16" @click="clean"></uni-icons>
		</view>
	</view>
	<!---历史列表-->
	<view class="history-box" v-else>
		<view class="history-title">
			<text>搜索历史</text>
			<uni-icons type="trash" size="17"></uni-icons>
		</view>
		<!--列表区域-->
		<view class="history-list" >
			<uni-tag  :text="item" v-for="(item ,i ) in historyList" :key="i" @click="gotoGoodsList(item)"></uni-tag>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//延时器的timerId
				timer:null,
				///搜索关键词
				kw:'',
				///搜索结果列表
				searchReuslts:[],
				///历史列表
				historyList:[]
			};
		},
		onLoad() {
			let sotres=uni.getStorageSync('kw')||'[]';
			this.historyList=JSON.parse(sotres);
		},
		
		computed:
		{
			historys()
			{
				return [... this.historyList].reverse()
			}
			
		}
		,
		methods:{
			input(e)
			{
				
				//清楚timer对应的延时器
				clearTimeout(this.timer);
				//重新启动一个延时器，并把timerId赋值给this.timer
				this.timer=setTimeout(()=>
				{
					//e是最新的搜索内容
					this.kw=e;
					this.getSearchList()
				},500)
			},
			//搜关键此，或者商品建议列表
			async getSearchList()
			{
				//判断内容是否为空
				if(this.kw==='')
				{
					this.searchReuslts=[]
					return 
				}
				 const res= await uni.$http.get("/api/public/v1/goods/qsearch",{query:this.kw})
				if(res.statusCode!==200)
				{
					return uni.$showMsg();
				}
				this.searchReuslts=res.data.message;
				//查询到搜索建议之后，调用saveSearchHistory()方法保存搜索关键字
				this.saveSearchHistory();
			},
			///保存搜索的关键词的方法
			saveSearchHistory()
			{
						///把搜索关键词push到 history列表
				//this.historyList.push(this.kw)
				///将array转换成Set
				let set = new Set(this.historyList);
				///删除历史的
				set.delete(this.kw)
				///添加新的
				set.add(this.kw)
				///Set 对象转换成Array
				this.historyList=Array.from(set)
				///对搜索历史数据，进行持久化的存储
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			gotoDetail(gotoDetail)
			{
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+gotoDetail.goods_id
				})
			},
			clean(){
				this.historyList=[];
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			gotoGoodsList(kw)
			{
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+kw
				})
			}
			
			
		}
	}
</script>

<style lang="scss">
.search-box
{
	position: static;
	top: 0;
	z-index: 999;
	
}
.sugg-list
{
	padding: 0 5px;
	.sugg-item
	{
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.goods-name
		{
			//问题不允许换行（d单行文本）
			white-space: nowrap;
			///溢出部门分隐藏
			overflow: hidden;
			//文本溢出后，使用...代替
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
	
}

.history-box
{
	padding: 0 5px;
	.history-title{
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 40px;
		font-size: 13px;
		border: 1px solid #efefef;
	}
	.history-list
	{
		display: flex;
		flex-wrap: wrap;
		.uni-tag
		{
			margin-top: 5px;
			margin-right: 5px;
		}
	}
}
</style>
